<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

use App\Comment;
use Session;

class CommentController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth',['except'=>'store']);
    }

    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name'=>'required|max:255',
            'email'=>'email',
            'comment'=>'required|max:255'
            ],[
            'name.required'=>'必填项',
            'email.email'=>'邮箱格式不正确',
            'comment.required'=>'评论必填',
            'max'=>'超过字数最大限制'
        ]);

        $comment = new Comment();
        $comment->name  = $request->name;
        $comment->email = $request->email;
        $comment->comment  = $request->comment;
        $comment->post_id  = $request->post_id;
        $comment->approved = true;
        $comment->save();
        // $comment->post()->associate($post);

        Session::flash('success', '发表评论成功');
        return redirect()->back();
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        $comment = Comment::find($id);
        return view('comment.edit')->withComment($comment);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $this->validate($request,[
            'comment'=>'required|max:255'
            ],[
            'required'=>'必须填写',
            'max'=>'不能超过:max个字符'
            ]);
        $comment = Comment::find($id);
        $comment->comment = $request->comment;
        $comment->save();

        Session::flash('success', '评论修改成功');
        return redirect()->route('post.show', $comment->post_id);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $comment = Comment::find($id);
        $post_id = $comment->post_id;
        $comment->delete();

        Session::flash('success', '已删除评论');
        return redirect()->route('post.show', $post_id);
    }
}
